package com.zhentao.controller;

import com.zhentao.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/*
 *云计算
 *魏子纹
 *2025/6/10
 */
@Controller
public class FreemarkerTestController {

    //freemarker. configuartion
    @Autowired
    private Configuration configuration;


    @RequestMapping("/t1")
    public String t1(Model model){
        // 传递一个文本数据
        model.addAttribute("name","测试使用-小明");

        Student student = new Student();
        student.setAge(1);
        student.setName("Yui");
        student.setBirthday(new Date());
        model.addAttribute("student",student);


        // 跳转到下一个页面
        return "01-test";
    }


    @ResponseBody
    @RequestMapping("/createHtml")
    public String createHtml() throws IOException, TemplateException {
        Template template = configuration.getTemplate("01-test.ftl");

        Student student = new Student();
        student.setName("小明");
        student.setAge(11);



        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(12);

//        //list
//        List list = new ArrayList();
//        list.add(student);
//        list.add(student2);
//
//
        // 传递的时候用 map传递，此时键和 ftl文件中的${}键一致
        Map<String,Object> map=new HashMap<>();
//        map.put("stus",list);
//
        map.put("student",student);

        template.process(map,new FileWriter("E://IdeaCodeTest//zhuangao4//ProjectNews//zhentao-leadnews//testGIt//01.html"));


        return "success";
    }


}
